import {defineStore} from "pinia"
import {ref} from "vue"
import router from "@/router/index.js"
// 引入发起请求菜单数据的方法
import {getMenuListApi} from "@/api/permission.js"

// 引入完整创建路由表
import allRoutes from "@/router/dynamicRoutes.js"

// 引入递归创建路由的方法
import recursionRoutes from "@/utils/recursionRoutes.js"
// 引入commonRoutes
import commonRoutes from "@/router/commonRoutes.js"
export default defineStore("menulist",()=>{
    // 控制菜单的展开和收起
    let isCollapse = ref(false)
    let menulist = ref([])

    // 获取后台菜单数据的方法
    let getMenuList = ()=> new Promise( async (resolve,reject)=>{
        // console.log(1);
        let res = await getMenuListApi()

        //    menulist.value =res.data.menuList;
    
          let userRoutes =  recursionRoutes(allRoutes,res.data.menuList)
    
          commonRoutes.children = userRoutes;
          // 将路由动态的添加到路由配置中
    
          router.addRoute(commonRoutes) 
          // 给菜单赋值
          menulist.value = userRoutes;

          resolve()
    })
    return {menulist,getMenuList,isCollapse}
})